<?php
namespace Home\Controller;

class OrderController extends CommonController {
    public function index(){
        $order = D('Orders');
        $total=$order->count();
        $page=new \Think\Page($total,10);
        $goods = D("Goods");
        //通过已登录用户的id获取订单表中信息
        $info = $order->order('id desc')->where("u_id={$_SESSION['login']['id']}")->limit($page->firstRow.','.$page->listRows)->select();
        $pageButton=$page->show();
        $detail = D('Detail');

        foreach($info as $key=>$val){
            //获取详情表中信息
            $list=$detail->where("orderid='{$val['id']}'")->select();

            foreach($list as $k=>$v){
                $list[$k]['picname'] = $goods->where("id=".$v['goodsid'])->find()['picname'];
                $list[$k]['addtime'] = $order->where("id=".$v['orderid'])->find()['addtime'];
                $list[$k]['total'] = $order->where("id=".$v['orderid'])->find()['total'];
                $list[$k]['orderstatus'] = $order->where("id=".$v['orderid'])->find()['status'];

            }
            //将订单表和详情表中需要展示出来的信息合并
            $detaillist[] = $list;
        } 
        //dump($detaillist);

        $status = array('新订单','已发货','已收货','无效订单');
        foreach($detaillist as $key=>$val){
            foreach($val as $k=>$v){
                $detaillist[$key][$k]['orderstatus'] = $status[$v['orderstatus']];
                date_default_timezone_set("PRC");  //设置中国时区
                $detaillist[$key][$k]['addtime'] = date('Y-m-d H:i:s',$detaillist[$key][$k]['addtime']);
            }
        }
        if(empty($detaillist))$detaillist[]['sg'] =1;
        //dump($detaillist);
        $this->assign('pageButton',$pageButton);
        $this->assign('detaillist',$detaillist);
        $this->display();
    }

    public function shouhuo(){
        $id = $_GET['id'];
        $order = D('Orders');
        $detaillist['status'] = 2;
        $order->where("id={$id}")->save($detaillist);

        $this->redirect('index');
    }

    public function detail(){
        $data['orderid'] = $_GET['id'];
        $detail = D('Detail');
        $list = $detail->where($data)->select();
        $total = $detail->where($data)->count();

        //dump($list);

        $this->assign('list',$list);
        $this->assign('total',$total);
        $this->display('detail');
    }
    //取消订单
    public function cancel(){
        $id = $_GET['id'];
        $order = D('Orders');
        $detaillist['status'] = 3;
        $order->where("id=".$id)->save($detaillist);
        $this->redirect('index');
    }

    

}
